package stats;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorHashMapStatsService implements IStatsService
{
	private ExecutorService			executor	= Executors.newSingleThreadExecutor();
	private HashMap<String, Long>	stats;

	public ExecutorHashMapStatsService ()
	{
		init();
	}

	private void init ( )
	{
		stats = new HashMap<String, Long>(5000);
	}

	public void notify ( final String key )
	{
		Runnable r = new Runnable()
		{
			public void run ( )
			{
				Long value = stats.get(key);
				if (value == null)
				{
					stats.put(key, 1L);
				}
				else
				{
					stats.put(key, ++value);
				}
			}
		};
		executor.execute(r);
	}

	public Map<String, Number> getStats ( )
	{
		return new HashMap<String, Number>(stats);
	}

	public void stop ( )
	{
		executor.shutdown();
		while (!executor.isTerminated())
		{

		}
	}
}
